﻿@model IEnumerable<Bibilioteca.Models.Material>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>Codigo</th>
        <th>Titulo</th>
        <th>Autor(es)</th>
        <th>Catalogo</th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>@Html.DisplayFor(modelItem => item.Codigo)
        </td>

        <td>@Html.DisplayFor(modelItem => item.Titulo)
        </td>

        <td>@{  bool primerAutor = true;
                string[] autoresArray = item.Autores.Split(',');
                string autoresString = "";
                
            foreach (var tipo in ViewBag.Autores as List<SelectListItem>)
            {
                if (autoresArray.Contains(tipo.Value))
                {
                    if (primerAutor) { primerAutor = false; }
                    else
                    {
                        autoresString += ", ";
                    }
                    autoresString += tipo.Text;
                }
            }
            @Html.DisplayFor(modelItem => autoresString);
        }
        </td>
        
        <td>@foreach (var tipo in ViewBag.Catalogos as List<SelectListItem>)
            {
                if (tipo.Value == item.SelectedCatalogo.ToString())
                {
                    @Html.DisplayFor(modelItem => tipo.Text);
                                                            break;
                }
            }
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
            @Html.ActionLink("Details", "Details", new { id = item.Id }) |
            @Html.ActionLink("Delete", "Delete", new { id = item.Id })
        </td>
    </tr>
}

</table>
